﻿using System;
using System.Data;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;
using ECP.Model;
using ECP.Utilities;
using DevExpress.XtraEditors;

namespace ECP.Client
{
    public partial class FrmException : FormBase
    {
        private DataTable DTMail;
        public BaseExceptionEntity ExceptionEntity;
        public BaseUserInfo ExceptionUserInfo;

        public FrmException()
        {
            this.DTMail = new DataTable();
            InitializeComponent();
        }

        public FrmException(BaseExceptionEntity exceptionEntity)
            : this()
        {
            this.ExceptionEntity = exceptionEntity;
        }

        public FrmException(Exception exception)
            : this()
        {
            this.ExceptionEntity = this.ConvertException(exception);
        }

        public FrmException(BaseUserInfo UserInfo, Exception Exception)
            : this(Exception)
        {
            base.UserInfo = UserInfo;
        }

        private void FrmException_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            this.ShowEntity();
            this.Cursor = Cursors.Default;
        }

        private void SendErrorReport()
        {
            if (BaseSystemInfo.ErrorReportTo.Length != 0)
            {
                string[] strArray = BaseSystemInfo.ErrorReportTo.Split(new char[] { ',', ';', ' ' });
                MailMessage message = new MailMessage();
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (strArray[i].Trim().Length > 0)
                    {
                        message.To.Add(strArray[i].Trim());
                    }
                }
                if (message.To.Count > 0)
                {
                    message.Subject = "Error From " + this.txtCustomer.Text + "(" + this.ExceptionUserInfo.IPAddress + ")";
                    message.Body = this.txtException.Text;
                    message.IsBodyHtml = false;
                    message.Priority = MailPriority.Normal;
                    SmtpClient client = new SmtpClient(BaseSystemInfo.ErrorReportMailServer);
                    NetworkCredential credential = new NetworkCredential(BaseSystemInfo.ErrorReportUsername, BaseSystemInfo.ErrorReportPassword);
                    client.UseDefaultCredentials = true;
                    client.Credentials = credential;
                    MailAddress address = new MailAddress(BaseSystemInfo.ErrorReportMailAddress);
                    message.From = address;
                    client.Send(message);
                }
            }
        }

        private void ShowEntity()
        {
            this.txtCustomer.Text = BaseSystemInfo.CustomerCompanyName;
            this.txtSoft.Text = BaseSystemInfo.SoftFullName;
            this.txtUser.Text = this.ExceptionEntity.CreateUserRealName;
            this.txtUser.Tag = this.ExceptionEntity.CreateUserID;
            this.txtOccurTime.Text = this.ExceptionEntity.CreateDate;
            this.txtException.Text = this.ExceptionEntity.Message + Environment.NewLine + this.ExceptionEntity.FormattedMessage;
        }

        private BaseExceptionEntity ConvertException(Exception exception)
        {
            return new BaseExceptionEntity { Message = exception.Message, FormattedMessage = exception.Source, CreateDate = DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat), CreateUserID = base.UserInfo.ID, CreateUserRealName = base.UserInfo.Realname };
        }

        private void btnPrintScreen_Click(object sender, EventArgs e)
        {
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            try
            {
                this.SendErrorReport();
                base.Close();
            }
            catch
            {
               XtraMessageBox.Show("发送错误报告邮件失败.", AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}